package com.yaofeng.mybatis.modules.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import com.yaofeng.mybatis.modules.sys.service.IUserRoleService;
import com.yaofeng.mybatis.modules.sys.entity.UserRole;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yaofeng.mybatis.annotation.Log;
import com.yaofeng.mybatis.common.PageInfo;
import com.yaofeng.mybatis.common.ResponseInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户和角色关联表 前端控制器
 * </p>
 *
 * @author yaofeng
 * @since 2019-11-01
 */
@RestController
@RequestMapping("/sys/user-role")
public class UserRoleController {

    @Autowired
    private IUserRoleService userRoleService;

    @GetMapping("/list")
    @RequiresAuthentication
    @Log(value = "列表")
    public ResponseInfo list(@RequestParam Map<String, Object> params, @RequestParam(defaultValue = "1") Long pageNum, @RequestParam(defaultValue = "50") Long pageSize) {
        IPage page = userRoleService.page(new Page<>(pageNum, pageSize), Wrappers.<UserRole>lambdaQuery());
        return ResponseInfo.success(new PageInfo(page));
    }

     /**
     * 保存
     * @param userRole
     * @return
     */
    @PostMapping("/save")
    @RequiresAuthentication
    @Log(value = "保存")
    public  ResponseInfo save(@RequestBody UserRole userRole){
    userRoleService.save(userRole);
        return ResponseInfo.success();
    }


    /**
     * 更新
     * @param userRole
     * @return
     */
    @PutMapping("/update")
    @RequiresAuthentication
    @Log(value = "更新")
    public  ResponseInfo update(@RequestBody  UserRole userRole){
    userRoleService.updateById(userRole);
        return ResponseInfo.success();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping ("/delete")
    @RequiresAuthentication
    @Log(value = "删除")
    public  ResponseInfo delete(Long id){
       userRoleService.removeById(id);
        return ResponseInfo.success();
    }




}
